package com.wkf.truck.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkf.truck.common.PageModel;
import com.wkf.truck.common.R;
import com.wkf.truck.dto.UserDto;
import com.wkf.truck.model.Address;
import com.wkf.truck.model.User;
import com.wkf.truck.service.AddressService;
import com.wkf.truck.service.UserService;
import com.wkf.truck.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lxb
 * @since 2021-02-14
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AddressService addressService;

    /**
     * 编辑用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/editMes")
    public R edit(@RequestBody User user) {
        userService.saveOrUpdate(user);
        return R.ok();
    }

    /**
     * 收获地址列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/getAddressList")
    public R getAddressList(@RequestParam("userId") Long userId) {

        List<AddressVo> list = addressService.getAddressList(userId);
        return R.ok().put("data", list);
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserInfo")
    public R getUerInfo(@RequestParam("userId") Long userId) {
        User user = userService.getOne(new QueryWrapper<User>().eq("id", userId));
        return R.ok().put("data", user);
    }

    /**
     * 校验用户是否已注册
     *
     * @param openId
     * @return
     */
    @GetMapping("/checkRegister")
    public R checkRegister(@RequestParam("openId") String openId) {

        User user = userService.getOne(new QueryWrapper<User>().eq("open_id", openId));
        if (user == null) {
            return R.ok().put("status", false);
        }
        return R.ok().put("status", true);
    }

    /**
     * 用户首次登录
     *
     * @param phone
     * @param openId
     * @param username
     * @param roleCode
     * @return
     */
    @GetMapping("/firstLogin")
    public R firstLogin(@RequestParam("phone") String phone, @RequestParam("openId") String openId, @RequestParam("username") String username, @RequestParam("roleCode") Long roleCode) {

        User user = new User();
        user.setOpenId(openId);
        user.setPhone(phone);
        user.setUsername(username);
        user.setRoleId(roleCode);

        userService.save(user);
        return R.ok();
    }

    /**
     * 用户注册
     *
     * @param userDto
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody UserDto userDto) {

        R r = userService.register(userDto);
        return r;
    }

    /**
     * 获取用户id
     *
     * @param openId
     * @return
     */
    @GetMapping("getUserId")
    public R getUserId(@RequestParam("openId") String openId) {

        User user = userService.getOne(new QueryWrapper<User>().eq("open_id", openId));

        if (user == null) {
            return R.error("不存在该用户");
        }
        return R.ok(user.getId());
    }

    /**
     * 获取用户
     * @param pageModel
     * @return
     */
    @PostMapping("/getNormalUsers")
    public R getNormalUsers(@RequestBody PageModel pageModel){
        Page<User> page = new Page<>(pageModel.getPageNum(),pageModel.getPageSize());
        IPage<User> list = userService.page(page);
        return R.ok(list);

    }
}
